﻿ package code
{

	import flash.display.*;
	import gemini.utils.*;
	import flash.geom.*;
	import caurina.transitions.*;
	import code.*;
	import flash.events.*;
	import flash.external.*;
	import flash.net.*;
	import code.ModuleBase;
	import code.interfaces.IDesqCore;
	public class Main extends Sprite implements IDesqCore
	{
		/*
		*	USER AND FACEBOOK VARS 
		*/
		public var owner;
		public var user;
		//public var friends;
		
		///
		///
		///
		//
		public var tempLoad;
		private  var _config;
		public var path="";
		private var _container;
		private var _container1;
		private var _fixed;
		var desk;
		public var backimg=null;;
		public var mods;
		public var lang;
		public var nav;
		public var navdata;
		var modinfo;
		var speed=.6;
		public var focusat;
		private var _internals={};
		//public var _admin;
		//public var _tutorials;
		var loadtxt;
		var dragging;
		public var back
		public var focusing=0;
		public var _loc=null;
		
		/*
		*
		*
		*/
		public var videoPlayer;
		public var flashPlayer;
		public var youtubePlayer;
		
		//
		//
		//
		public function Main()
		{
			owner=1097635733;
			user=
			{
				permission:0
			}
			getLocation();			
			loadConfig();
			
		}
		private function onprog(e)
		{
			var frm=Math.round((e.loadedsize/e.totalsize)*100);
			ldscale.scl.gotoAndStop(frm);
			ldscale.txt.text=loadtxt+frm+"%";
			
		}
		private function loadConfig()
		{
			trace("laoding config");
			var ld=new LoadJSONs();
			ld.addFile("php/config.php",this,"tempLoad");
			//ld.addFile("php/nav.php",this,"navdata");
			//ld.addFile("php/friends.php",this,"friends");
			//ld.addFile("php/user.php",this,"user");
			ld.onComplete(setPermisions);			
			ld.loadFiles();
		}
		
		
		private function setPermisions(e)
		{
			
			_config=tempLoad;
			tempLoad=null;
			//trace("------>"+user.uid)
			/******
			*	CHECK USER LOGGED
			*/
			/*
			if(String(user.uid)=="undefined")
			{
				trace("**************** user not logged *****************");
				user.permission=2;
				
			}
			else
			{
				if(user.uid==owner)
				{
					trace("********** OWNER **********");
					user.permission=0;
				}
				else
				{
					trace("********** FRIEND ************");
					user.permission=1;
				}
			}*/
			loadModules(0);
		}
		private function loadModules(e)
		{
			//trace("loading modules");
			loadtxt="Loading myDesq ";
			var ld=new BulkLoader();
			ld.onProgress(onprog);
			
			ld.addFile("admin/admin.swf",_internals,"_admin");
			ld.addFile("tutorials/tutorials.swf",_internals,"_tutorials");
			if (config.props.backimg != "")
			{
				//trace("got back");
				ld.addFile(config.props.backimg,this,"backimg");
			}
			for (var n=0; n<config.modules.length; n++)
			{
				//ld.addFile("modules/"+config.modules[n].clip,config.modules[n],"clip");
			}
			ld.onComplete(preloadingDone);
			ld.loadFiles();
		}
		private function preloadingDone(e)
		{
			//init(0);
			teaser.gotoAndStop("end");
			Tweener.addTween
			(
			 	teaser,
				{
					alpha:0,
					time:.3,
					delay:.1,
					transition:"linear",
					onComplete:removeTeaser
				}
			 )
			Tweener.addTween
			(
			 	ldscale,
				{
					alpha:0,
					time:.3,
					transition:"linear"
					
				}
			 )
			
		}
		public function removeTeaser()
		{
			removeChild(teaser);
			removeChild(ldscale);
			init(0);
		}
		private function init(e)
		{
			
			dragging=0;
			
			lang = config.props.langs[1];
			_container=new Sprite();
			addChildAt(_container,0);
			_container1=new frnt();
			addChildAt(_container1,1);
			_fixed=new Sprite();
			addChildAt(_fixed,2);
			
			//Filters.addFilters(_container1,"BlurFilter,quality:3");
			//var fil=new filterclip();
			//addChild(fil);
			
			//nav = new Nav(this);
			//nav.x = 5
			//nav.y = 5;
			//addChild(nav);
			//nav.init();
			//Filters.addFilters(nav,"DropShadowFilter,quality:3,distance:2,strength:.8,blurX:3,blurY:3");
			makeInfo();
			makeDesktop();//<<-------------------------------------------------------------------------------------
			makeNav();
			makeTutorials();
			
			
			
			
		}
		/*
		*
		*
		*
		*
		*/
		public function setBackimg(lnk)
		{
			trace(" ------ loading back img "+lnk);
			var ld=new BulkLoader();
			ld.addFile(lnk,this,"backimg");
			ld.onComplete(makeBackground);
			ld.loadFiles();
		}
		private function makeNav()
		{
			nav = new Nav(this);
			
			addChildAt(nav,2);
			nav.init();
		}
		private function makeTutorials()
		{
			addChild(_internals._tutorials);
			_internals._tutorials.mainframe=this;
						
		}
		private function makeInfo()
		{
			modinfo=new moduleinfo();
			modinfo.visible=false
			modinfo.x=mouseX;
			modinfo.y=mouseY;
			addChild(modinfo);
		}
		public function makeModule(md)
		{
			trace("making module "+md);
			var n=mods.length;
			mods[n]=new DeskModule(this);
			mods[n].mod=md
			//md.mainframe=this;
			//mods[n].mynum=n;
			//config.modules[n].clip.mainframe=this;
			//mods[n].addChild(md);
			
			//md.x =  -  md.width / 2;
			//md.y =  -  md.height / 2;
			mods[n].x=desk.mouseX;
			mods[n].y=desk.mouseY;
			mods[n].init();
			//mods[n].startDrag(true);
			_internals._admin.addArrow(mods[n]);
			
			desk.addChild(mods[n]);
			
			
		}
		private function makeDesktop()
		{
			desk=new Sprite();

			mods = [];

			for (var n=0; n<config.modules.length; n++)
			{
				mods[n]=new DeskModule(this);
				mods[n].mod=config.modules[n]
				mods[n].delay=(n+1)*200;
				//mods[n].mynum=n;
				//config.modules[n].clip.mainframe=this;
				//mods[n].addChild(config.modules[n].clip);
				//config.modules[n].clip.x =  -  config.modules[n].clip.width / 2;
				//config.modules[n].clip.y =  -  config.modules[n].clip.height / 2;
				mods[n].x=config.modules[n].x
				mods[n].y=config.modules[n].y
				mods[n].ssx=mods[n].x;
				mods[n].ssy=mods[n].y;
				//mods[n].x=50+((mods[n].width)+30)*n+200;
				//mods[n].y=50+(mods[n].height)+50*n;
				//mods[n].y=Math.random()*900+50
				mods[n].rotation = Math.random() * 10 - 5;
				//mods[n].cacheAsBitmap=true;
				mods[n].init();
				desk.addChild(mods[n]);
				//Filters.addFilters(mods[n],"DropShadowFilter,quality:3,distance:1,strength:.8,blurX:3,blurY:3");
			}
			/*
			*
			*/
			trace(desk.width,desk.height);

			
			
			/*
			*
			*/
			
			_container.addChild(desk);			
			
			makeBackground()
			
			stage.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoved);
			//initModules();
			
			//focusFistNavModule();
		}
		
		public function makeBackground(e=null)
		{
			//trace(" * * *  MAKING BACK");
			try
			{
				//trace("removing back");
				_container.removeChild(back);
			}
			catch(e)
			{
				trace(e);
			}
			
			if (backimg != null)
			{
				//trace("MAKING BACK WITH "+backimg);

				back=new Sprite();
				back.alpha=1
				var m=new Matrix();
				m.rotate(-.15);
				m.scale(.8,.8);
				back.graphics.beginBitmapFill(backimg.bitmapData,m,1,1);
				back.graphics.drawRect(-1000,-1000,300+desk.width*1.8,300+desk.height*2);
				back.graphics.endFill();
				//back.cacheAsBitmap=true;
				_container.addChildAt(back,0);
				//back.addEventListener(MouseEvent.MOUSE_DOWN,dragBack);
				//back.addEventListener(MouseEvent.MOUSE_UP,dontDragBack);
				
				//back.addEventListener(MouseEvent.MOUSE_DOWN,focusOnMouse);
			}
		}
		private function initModules()
		{
			for(var n=0;n<mods.length;n++)
			{
				for(var prop in config.modules[n].params)
				{
					trace(prop);
					config.modules[n].clip[prop]=config.modules[n].params[prop];
				}
				try
				{
					config.modules[n].clip.init();
				}
				catch(e)
				{
					//trace(e);
				}
			}
		}
		//
		//
		public function dragBack(e)
		{
			//var rct=new Rectangle(-1000,-1000,_container.width-1000,_container.height-1000);
			//trace(_container.width,_container.height);
			_container.startDrag(false);
			dragging=1;
		}
		public function dontDragBack(e)
		{
			_container.stopDrag();
			dragging=0;
		}
		/*******************************************************************
		*
		*	PLAYERS START
		*
		*********************************************************************/
		
		public function playVideo(v)
		{
			
			videoPlayer.playVideo(v)
		}
		public function playYoutube(v)
		{			
			youtubePlayer.playYoutube(v)
		}
		public function playFlash(v,s)
		{
			//trace("playing flash "+v,"size "+s.w);
			
			flashPlayer.playFlash(v,s)
		}
		/**********************************************************
		*
		*
		*
		*
		************************************************************/
		public function reCenter(me,dims=null)
		{
			//var n=me
			//me=config.modules[n].clip
			me=me.clip;
			if(dims==null)
			{
				me.x=-me.width/2
				me.y=-me.height/2
			}
			else
			{
				me.x=-dims.width/2
				me.y=-dims.height/2
			}
		}
		/*
		*
		*
		*
		*/
		public function showInfo(e)
		{
			//modinfo.rotation=Math.random()*4-2;
			
			//trace("SHOWING INFO "+e);
			if(mouseX>400)
			{
				modinfo.gotoAndStop("left");
			}
			else
			{
				modinfo.gotoAndStop("right");
			}
			modinfo.tit.text=e.title[lang]
			modinfo.txt.text=e.info[lang]			
			modinfo.alpha=0
			;modinfo.visible=true;
			Tweener.addTween
			(
			 	modinfo,
				{
					alpha:1,
					time:.1,
					transition:"easeInOutQuad"
				}
			 
			 )
			
		}
		public function showShortInfo(e)
		{
			
			modinfo.gotoAndStop("short");
			modinfo.tit.text=e.title[lang]
			//modinfo.txt.text=e.info[lang]			
			modinfo.alpha=0
			modinfo.visible=true;
			Tweener.addTween
			(
			 	modinfo,
				{
					alpha:1,
					time:.1,
					transition:"easeInOutQuad"
				}
			 
			 )
			
		}
		public function showMediumInfo(e)
		{
			
			modinfo.gotoAndStop("medium");
			modinfo.tit.text=e.title[lang]+"\n"
			modinfo.txt.text=e.info[lang]		
			//trace(e.info,lang);
			modinfo.alpha=0
			modinfo.visible=true;
			Tweener.addTween
			(
			 	modinfo,
				{
					alpha:1,
					time:.1,
					transition:"easeInOutQuad"
				}
			 
			 )
			
		}
		public function hideInfo()
		{
			modinfo.visible=false;
		}
		private function mouseMoved(e)
		{
			modinfo.x=mouseX;
			modinfo.y=mouseY;
			if(dragging)
			{
				_container1.x=_container.x;
				_container1.y=_container.y;
			}
		}
		//
		//
		public function focusModule(num)
		{
			focusing=1;
			Tweener.updateTime();
			var dx = mods[num].x;
			var dy = mods[num].y;
			var rot = mods[num].rotation;
			
			focusat={x:dx,y:dy};
			

			Tweener.addTween(
			 _container,
			{
			x:400-dx,
			y:300-dy,
			//rotation:rot,
			time:speed,
			transition:"easeInOutQuad",
			onComplete:function(){focusing=0;}
			
			}
			 
			 );
			Tweener.addTween(
			 _container1,
			{
			x:400-dx*1,
			y:300-dy*1,
			//rotation:rot,
			time:speed,
			transition:"easeInOutQuad"
			
			}
			 
			 );
			setLocation(dx,dy)
			
			
		}
		public function focusByClip(clp)
		{
			
			Tweener.updateTime();
			focusing=1;
				focusat=clp;
				var dx = clp.x;
				var dy = clp.y;
				var rot = clp.rotation;
				
				focusat={x:dx,y:dy};
				
				Tweener.addTween(
				 _container,
				{
				x:400-dx,
				y:300-dy,
				//rotation:rot,
				time:speed,
				transition:"easeInOutQuad",
				onComplete:function(){focusing=0;}
				
				}
				 
				 );
				Tweener.addTween(
				 _container1,
				{
				x:400-dx*1,
				y:300-dy*1,
				//rotation:rot,
				time:speed,
				transition:"easeInOutQuad"
				
				}
				 
				 );
			setLocation(dx,dy)
			
		}
		
		
		public function focusOnPos(dx,dy,quick=0)
		{
			focusing=1;
			if(!quick)
			{
			Tweener.updateTime();
				
	
				Tweener.addTween(
				 _container,
				{
				x:400-dx,
				y:300-dy,
				//rotation:rot,
				time:speed,
				transition:"easeInOutQuad",
				onComplete:function(){focusing=0;}
				
				}
				 
				 );
				Tweener.addTween(
				 _container1,
				{
				x:400-dx*1,
				y:300-dy*1,
				//rotation:rot,
				time:speed,
				transition:"easeInOutQuad"
				
				}
				 
				 );
				focusat={x:dx,y:dy};
			}
			else
			{
				_container.x=400-dx;
				_container.y=300-dy;
				_container1.x=_container.x;
				_container1.y=_container.y;
			}
			setLocation(dx,dy)
			
			
		}
		
		
		private function focusOnMouse(e)
		{
			focusOnPos(_container.mouseX,_container.mouseY)
		}
		public function setToFront(c)
		{
			
			//var fac_x=_container1.x-_container.x;
			//var fac_y=_container1.y-_container.y;
			_container1.addChild(c);
			//c.ssx-=fac_x;
			//c.ssy-=fac_y;
			
		}
		public function setToBack(c)
		{
			
			_container.addChild(c);
			/*c.x=c.ssx;
			c.y=c.ssy;
			*/
		}
		/*
		*
		*
		*/
		public function setLocation(lx,ly)
		{
			
			var rr = new URLRequest("#loc:" +lx+","+ly);
			//navigateToURL(rr,"_self");
			
		}
		public function getLocation()
		{
			trace(ExternalInterface);
			//txt.text="Getting location";
			
			/*var cl=ExternalInterface.call("getUrl");
			if(cl.indexOf("#")!=-1)
			{				
				
				var str=cl.substr(cl.indexOf("#"),cl.length);
				str=str.substr(str.indexOf(":")+1,str.length);
				
				_loc={};
				_loc.x=int(str.substr(0,str.indexOf(",")));
				_loc.y=int(str.substr(str.indexOf(",")+1,str.length));
				txt.text="> "+_loc.x+":"+_loc.y;
			}
			else
			{
				_loc=null;
			}
			*/
			
		}
		/*
		*
		*
		*/
		public function get location():Object
		{
			return _loc;
		}
		public function get container():Sprite
		{
			return _container;
		}
		public function get container1():Sprite
		{
			return _container1;
		}
		public function get fixed():Sprite
		{
			return _fixed;
		}
		public function get admin():Object
		{
			return _internals._admin;
		}
		public function get config():Object
		{
			return _config;
		}
		
		/*
		*
		*
		*
		*
		*/
		public function getClass(lib_mc:MovieClip, classLib:String):Class
		{
			return lib_mc.loaderInfo.applicationDomain.getDefinition(classLib)  as  Class;
		}

	}

}